package com.elong.android.specialhouse;

import android.app.Application;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.SDKInitializer;
import com.dp.android.elong.crash.ActivityCrashHandler;
import com.dp.android.elong.crash.LogWriter;
import com.dp.android.elong.crash.support.LogConfig;
import com.elong.android.specialhouse.account.Account;
import com.elong.android.specialhouse.utils.BDLocationManager;
import com.elong.android.specialhouse.utils.FileUtils;
import com.elong.android.specialhouse.utils.ToastUtils;
import com.elong.android.specialhouse.utils.Utils;
import com.elong.framework.net.request.BaseRequestOption;
import com.elong.framework.netmid.NetConfig;
import com.elong.framework.netmid.process.ISessionClient;
import com.elong.mobile.countly.ElongCountly;
import com.elong.mobile.countly.support.CountlyConfig;
import com.elong.mobile.countly.support.GPSPoint;
import com.elong.mobile.countly.support.ICountlySupport;
import com.elong.myelong.usermanager.User;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

// import com.facebook.stetho.Stetho;

/**
 * Created by kaiqi.li on 2015/8/12.
 */
public class ApartmentApplication extends Application {

    public static final String TAG = "ApartmentApplication";
    private static ApartmentApplication mContext;

    public static ApartmentApplication getContext() {
        if (mContext == null) {
            mContext = new ApartmentApplication();
        }
        return mContext;
    }

    @Override
    public void onCreate() {

        super.onCreate();
        Log.e("ApartmentApplication","ApartmentApplication");
        mContext = this;
        //听云初始化
//        NBSAppAgent.setLicenseKey(AppConstants.TINGYUN_KEY).withLocationServiceEnabled(true).start(this);

//        Stetho.initialize(
//                Stetho.newInitializerBuilder(this)
//                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
//                        .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
//                        .build());

        // 百度定位初始化
        BDLocationManager.getInstance().init(this);

        // 百度地图SDK初始化
        SDKInitializer.initialize(this);

        // 初始化Toast
        ToastUtils.init(this);

        // Initialize Imageloader
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .memoryCache(new WeakMemoryCache())
                .memoryCacheSize(2 * 1024 * 1024)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                        // TODO .imageDownloader(new extendedi)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                // .enableLogging()
                .build();
        ImageLoader.getInstance().init(config);

        //用户信息初始化
        Account.init(this);
        //初始化app所需文件
        FileUtils.initTempPicFilePath();
        /**
         * Initialize the Network Framework
         */
        NetConfig.init(this);
        NetConfig.setApiKey(com.elong.android.specialhouse.AppConstants.NEW_API_SECRET_KEY);
        NetConfig.setCompressType(BaseRequestOption.CompressType.GZIP);
        NetConfig.setSessionClient(new ISessionClient() {
            @Override
            public String getSession() {
                return Account.getInstance().getAccessToken();
            }

            @Override
            public String getDeviceId() {
                return Utils.getDeviceID(getContext());
            }
        });
        initLog();
        initCountly();
        // 初始化crash处理
        ActivityCrashHandler.getInstance().init(this);

        //支付相关初始化
//        PaymentHelper.initPayment(getContext());
    }

    private void initLog() {
        LogConfig config = new LogConfig();
        config.openAutoTest = IConfig.AUTO_TEST_ON;
        config.openDebug = IConfig.DEBUG_ON;
        config.openMonitor = AppConstants.isMonitor;
        config.openSendHttpException = AppConstants.isSendHttpsExceptionOpen;
        config.WRITE_LOG_TO_WHERE = IConfig.WRITE_LOG_TO_WHERE;
        LogWriter.config(config);
    }
    private void initCountly() {
        CountlyConfig config = new CountlyConfig(this,getString(R.string.app_name)
                , AppConstants.SERVER_URL, AppConstants.COUNTLY_KEY , new ICountlySupport() {
            @Override
            public User getUser() {
                //必须不为空
                return User.getInstance();
            }
            @Override
            public String getPushId() {
                return Utils.getPushIDFromLocal();
            }
            @Override
            public GPSPoint getGPSPoint() {
                try{
                    BDLocation point = BDLocationManager.getInstance().mCurrentLocation;
                    if(point != null){
                        GPSPoint gpspoint = new GPSPoint(point.getLatitude(), point.getLongitude());
                        return gpspoint;
                    }
                }catch(Exception e){
                }
                return null;
            }
            @Override
            public String getDeviceId() {
                return Utils.getDeviceID(ApartmentApplication.getContext());
            }
            @Override
            public String getBizChannel() {
                return AppConstants.COUNTLY_ROOT_TEXT;
            }
            @Override
            public boolean isNeedWriteCountlyDataToLocaltxt() {
                return false;
            }
        });
        config.setDebug(!IConfig.COUNTLY_ON);
        config.setClientType(AppConstants.DEFAULT_CLIENTTYPE);
        ElongCountly.init(config);
    }
}
